package com.itheima.web.manager;

import com.itheima.service.db.AnalysisByDayService;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AnalysisManager {

    @Reference
    private AnalysisByDayService analysisByDayService;


    //概要统计
    public AnalysisSummaryVo summary() {
        return analysisByDayService.summary();
    }

    //新增、活跃用户、次日留存率
    public Map<String, List> countDashboard(String type, Long startTime, Long endTime) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //转换startTime日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(startTime);
        String beginTime = sdf.format(calendar.getTime());//开始时间
        System.out.println("开始时间:"+beginTime);

        //转换endTime日期格式
        calendar.setTimeInMillis(endTime);
        String overTime = sdf.format(calendar.getTime());//结束时间
        System.out.println("结束时间:"+overTime);

        //获取上一年开始时间
        Date date = sdf.parse(beginTime);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR,-1);//日期减1年
        String lastBeginTime = sdf.format(calendar.getTime());//上一年开始时间
        System.out.println("上一年开始时间:"+lastBeginTime);

        //获取上一年结束时间
        Date date1 = sdf.parse(overTime);
        calendar.setTime(date1);
        calendar.add(Calendar.YEAR,-1);//日期减1年
        String lastOverTime = sdf.format(calendar.getTime());//上一年结束时间
        System.out.println("上一年结束时间:"+lastOverTime);

        return analysisByDayService.countDashboard(type, beginTime, overTime, lastBeginTime, lastOverTime);
    }
}
